package com.example.demo.firemain.data;



import com.example.demo.firemain.ByteUtil;
import com.example.demo.firemain.NumConstant;
import lombok.Data;

import java.text.SimpleDateFormat;


/**
 * 郭设备状态表
 * 部件状态
 *
 * @author TDS
 * @date 2022/08/23
 */
@Data
public class DeviceStatusGuoBiao {

	private String device_id;

	private Integer status;

	private UserMessage userMessage;
	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public DeviceStatusGuoBiao(String message,String deviceId) {

		this.device_id = deviceId;


		userMessage = new UserMessage();
		userMessage.setSystemType(Integer.parseInt(message.substring(0, 2), 16));
		userMessage.setFireMainCode(Integer.parseInt(message.substring(2, 4), 16));
		userMessage.setPartType(Integer.parseInt(message.substring(4, 6), 16));
		userMessage.setAddressNo(String.valueOf(Integer.parseInt(message.substring(8, 10) +
				message.substring(6, 8), 16)));

		userMessage.setLoopNo(String.valueOf(Integer.parseInt(message.substring(12, 14) +
				message.substring(10, 12), 16)));
		String bit_stat = ByteUtil.buqi(Integer.toBinaryString(Integer.parseInt(message.substring(14, 18), 16)));
		status = 2;
		//0000 0001 0000 0100
		//0000 0010 0000 0000
		if (NumConstant.ONE.equals(bit_stat.substring(0, 1))) {
			userMessage.setPartStat("延时");
		} else if (NumConstant.ONE.equals(bit_stat.substring(1, 2))) {
			userMessage.setPartStat("反馈");
		} else if (NumConstant.ONE.equals(bit_stat.substring(2, 3))) {
			userMessage.setPartStat("启动");
		}  else if (NumConstant.ONE.equals(bit_stat.substring(3, 4))) {
			userMessage.setPartStat("监管");
		} else if (NumConstant.ONE.equals(bit_stat.substring(4, 5))) {
			userMessage.setPartStat("屏蔽");
		} else if (NumConstant.ONE.equals(bit_stat.substring(5, 6))) {
			userMessage.setPartStat("故障");
			status = 0;
		} else if (NumConstant.ONE.equals(bit_stat.substring(6, 7))) {
			userMessage.setPartStat("火警");
			status = 1;
		}

		else if (NumConstant.ONE.equals(bit_stat.substring(8, 9))) {
			userMessage.setPartStat("正常");
			//0000 0001 0000 0100
		}else if(NumConstant.ONE.equals(bit_stat.substring(9,10))){
			userMessage.setPartStat("停止");
		}else if(NumConstant.ONE.equals(bit_stat.substring(10,11))){
			userMessage.setPartStat("监管取消");
		}else if(NumConstant.ONE.equals(bit_stat.substring(11,12))){
			userMessage.setPartStat("屏蔽取消");
		}else if(NumConstant.ONE.equals(bit_stat.substring(12,13))){
			userMessage.setPartStat("反馈取消");
		}else if(NumConstant.ONE.equals(bit_stat.substring(13,14))){
			userMessage.setPartStat("故障恢复");
		}
		else if (NumConstant.ONE.equals(bit_stat.substring(14, 15))) {
			userMessage.setPartStat("火警恢复");
		} else if (NumConstant.ONE.equals(bit_stat.substring(15,16))){
			userMessage.setPartStat("电源故障");
			status = 0;
		}else{
			userMessage.setPartStat("正常");
		}
		//设备台账信息没有，解析出来都是空格
		String addressDesc = ByteUtil.gB18030ByteArrayToUtf8String(ByteUtil.hexStringToByteArray(message.substring(18, 80))).replace("\u0000", "").replace("\uE5E5", "");
		userMessage.setPartExplain(addressDesc);


		String time = message.substring(80);
		String ms = String.valueOf(Integer.parseInt(time.substring(0, 2), 16));
		String min = String.valueOf(Integer.parseInt(time.substring(2, 4), 16));
		String hour = String.valueOf(Integer.parseInt(time.substring(4, 6), 16));
		String day = String.valueOf(Integer.parseInt(time.substring(6, 8), 16));
		String month = String.valueOf(Integer.parseInt(time.substring(8, 10), 16));
		String year = String.valueOf(Integer.parseInt(time.substring(10), 16));

		userMessage.setUpTime("20" + year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + ms);


	}

}
